﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Security.AccessControl;
using System.Security.Policy;
using System.Text;
using System.Threading.Tasks;
using Aribaccio;
using Aribaccio.Security;
using BDPAutodetect.DataService;
using BDPAutodetect.Models;
using Newtonsoft.Json.Linq;
using static System.Runtime.InteropServices.JavaScript.JSType;

namespace BDPAutodetect.Mgrcore
{
    /// <summary>
    /// 系统参数管理
    /// </summary>
    public partial class SettingSystemManager : ObservableModel
    {
        /// <summary>
        /// 名称
        /// </summary>
        public string Name { get; set; }
        /// <summary>
        /// 基础数据操作实例
        /// </summary>
        protected IDataServiceBasic DataService { get; set; }
        /// <summary>
        /// 系统参数配置数据
        /// </summary>
        public List<MSettingSystem> SettingDatas { get; set; }
        /// <summary>
        /// 是否有效存在参数配置信息
        /// </summary>
        public bool IsValid => this.SettingDatas != null && this.SettingDatas.Count > 0;

        /// <summary>
        /// 构造函数[注入]
        /// </summary>
        public SettingSystemManager(IDataServiceBasic argDataService)
        {
            this.Name = "系统参数管理中心";
            this.DataService = argDataService;
            this.SettingDatas = new List<MSettingSystem>();
        }

        /// <summary>
        /// 初始化
        /// </summary>
        /// <returns></returns>
        public void Initialize()
        {
            //获取数据库数据
            var results = this.DataService.SelectModelItems<TBasicSystems>();
            if (results == null || !results.ResultState || results.Data == null || results.Data.Count <= 0) return;

            foreach (var item in results.Data)
            {
                if (item == null) continue;
                this.SettingDatas.Add(new MSettingSystem(item.ID, item.Name)
                {
                    Datum = (object)item.Datum,
                    DataType = item.DataType.ToEnum<DataType>()
                });
            }
        }

        /// <summary>
        /// 更新系统参数数据
        /// </summary>
        /// <param name="argType">系统参数枚举类型</param>
        /// <param name="argDatum">参数数据</param>
        /// <returns></returns>
        public async Task<bool> UpdateSettingData(SettingType argType, object argDatum)
        {
            if (!this.IsValid || argType == SettingType.None) return false;

            var data = this.SettingDatas.FirstOrDefault(v => v.Type == argType);
            if (data == null) return false;

            var tData = data.CloneTData();
            tData.Datum = argDatum.ToString();
            //更新数据库数据
            var result = this.DataService.UpdateWhereTask<TBasicSystems>(tData, v => v.ID == tData.ID);
            await result.ConfigureAwait(false);
            //更新成功，更新缓存数据
            if (result != null && result.Result != null && result.Result.ResultState)
            {
                data.Datum = argDatum;
                return true;
            }
            return false;
        }

        /// <summary>
        /// 获取系统参数数据
        /// </summary>
        /// <param name="argType"></param>
        /// <returns></returns>
        public MSettingSystem? GetSettingTypeData(SettingType argType)
        {
            if (!this.IsValid) return null;
            return this.SettingDatas.FirstOrDefault(v => v.Type == argType);
        }

        /// <summary>
        /// 获取系统参数数据
        /// </summary>
        /// <typeparam name="TData">数据值类型</typeparam>
        /// <param name="argType">系统参数枚举类型</param>
        /// <param name="argDefault">默认数据值</param>
        /// <returns></returns>
        public TData GetSettingTypeData<TData>(SettingType argType, TData argDefault) where TData : struct
        {
            var data = this.GetSettingTypeData(argType);
            return data != null? data.GetData(argDefault): argDefault;
        }

    }
}
